package fr.ralmn.OgameDroid.list.Adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import fr.ralmn.OgameDroid.R;
import fr.ralmn.OgameDroid.Object.Objects;
import fr.ralmn.OgameDroid.Object.Dependence.RequireItemBatiments;
import fr.ralmn.OgameDroid.Ressource.Listing;

public class RequireBArrayAdapter extends ArrayAdapter<RequireItemBatiments> {

	public class ViewHolder {
		public TextView nameView;
		public ImageView iconeView;
	}

	private ArrayList<RequireItemBatiments> technos;
	private LayoutInflater mInflater;
	private Context mContext;

	public RequireBArrayAdapter(Context context,
			ArrayList<RequireItemBatiments> techno) {
		super(context, R.id.require_lvl, R.id.require_name, techno);
		technos = techno;
		mInflater = LayoutInflater.from(context);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout layoutItem;
		if (convertView == null) {
			layoutItem = (LinearLayout) mInflater.inflate(
					R.layout.requierelist, parent, false);
		} else {
			layoutItem = (LinearLayout) convertView;
		}
		TextView name = (TextView) layoutItem.findViewById(R.id.require_name);
		TextView lvl = (TextView) layoutItem.findViewById(R.id.require_lvl);
		ImageView img = (ImageView) layoutItem.findViewById(R.id.require_img);
		img.setImageResource(technos.get(position).getItem().getImg());
		name.setText(technos.get(position).getItem().getName());
		lvl.setText(" " + technos.get(position).getLevel() + "");

		return layoutItem;
	}

	@Override
	public int getCount() {
		return technos.size();
	}

	@Override
	public RequireItemBatiments getItem(int position) {
		Objects.setR();
		return technos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static void autoScaleTextViewTextToHeight(final TextView tv, String s) {
		float currentWidth = tv.getPaint().measureText(s);
		int scalingFactor = 0;
		final int characters = s.length();
		// scale based on # of characters in the string
		if (characters < 5) {
			scalingFactor = 1;
		} else if (characters >= 5 && characters < 10) {
			scalingFactor = 2;
		} else if (characters >= 10 && characters < 15) {
			scalingFactor = 3;
		} else if (characters >= 15 && characters < 20) {
			scalingFactor = 3;
		} else if (characters >= 20 && characters < 25) {
			scalingFactor = 3;
		} else if (characters >= 25 && characters < 30) {
			scalingFactor = 3;
		} else if (characters >= 30 && characters < 35) {
			scalingFactor = 3;
		} else if (characters >= 35 && characters < 40) {
			scalingFactor = 3;
		} else if (characters >= 40 && characters < 45) {
			scalingFactor = 3;
		} else if (characters >= 45 && characters < 50) {
			scalingFactor = 3;
		} else if (characters >= 50 && characters < 55) {
			scalingFactor = 3;
		} else if (characters >= 55 && characters < 60) {
			scalingFactor = 3;
		} else if (characters >= 60 && characters < 65) {
			scalingFactor = 3;
		} else if (characters >= 65 && characters < 70) {
			scalingFactor = 3;
		} else if (characters >= 70 && characters < 75) {
			scalingFactor = 3;
		} else if (characters >= 75) {
			scalingFactor = 5;
		}

		// System.out.println(((int)Math.ceil(currentWidth)/tv.getWidth()+scalingFactor));
		// the +scalingFactor is important... increase this if nec. later
		while ((((int) Math.ceil(currentWidth) / tv.getWidth() + scalingFactor) * tv
				.getTextSize()) < tv.getHeight()) {
			tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, tv.getTextSize() + 0.25f);
			currentWidth = tv.getPaint().measureText(s);
			// System.out.println(((int)Math.ceil(currentWidth)/tv.getWidth()+scalingFactor));
		}

		tv.setText(s);
	}

}
